package com.smart.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.smart.common.constant.GlobalConstants;
import com.smart.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 *  MyMetaObjectHandler
 *  自动填充数据
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("createTime", new Date(), metaObject);
        try{
            setFieldValByName("createBy",SecurityUtils.getLoginUser().getUsername(), metaObject);
        }catch(Exception e){}
        setFieldValByName("delFlag", GlobalConstants.SUCCESS, metaObject);
        setFieldValByName("status", GlobalConstants.SUCCESS, metaObject);
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime", new Date(), metaObject);
        try{
            setFieldValByName("updateBy", SecurityUtils.getLoginUser().getUsername(), metaObject);
        }catch(Exception e){}
    }
}
